//
//  HREvaluationViewController.m
//  haierapp
//
//  Created by gaotangzhang on 14-9-3.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HREvaluationViewController.h"

@interface HREvaluationViewController (){
 
    NSMutableArray *_scoreArray;//商品评分
    NSMutableArray *_contentArray;//商品评价内容
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *button;

@end

@implementation HREvaluationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.tableView.backgroundColor=[UIColor clearColor];
    
    //设置按钮背景
    UIImage *bgImage=[[UIImage imageNamed:@"orange_btn"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    
    [self.button setBackgroundImage:bgImage forState:UIControlStateNormal];
    
    self.title=@"商品评分";
    
    //初始化分数与评价内容
    _scoreArray=[NSMutableArray array];
    _contentArray=[NSMutableArray array];
    
    int count=0;
    if (_order.productList!=nil && [_order.productList count]>0) {
        count=[_order.productList count];
    }
    
    for (int i=0; i<count; i++) {
        [_scoreArray addObject:@(0)];
        [_contentArray addObject:@""];
    }
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [self.tableView reloadData];
    
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Methods

//评价商品
- (IBAction)onTapEvaluate:(id)sender {
    
    BOOL isPass=YES;
    
    //检测是否评价商品
    for (NSString *content in _contentArray) {
        if ([content isEqualToString:@""]) {
            isPass=NO;
            break;
        }
    }
    
    if (!isPass) {
        [self showTextHUD:@"请填写商品的评价"];
        return;
    }
    
    //评价商品的参数
    NSMutableArray *productList=[NSMutableArray array];
    int count=0;
    if (_order.productList!=nil && [_order.productList count]>0) {
        count=[_order.productList count];
    }
    for (int i=0; i<count; i++) {
        HROrderProductInfo *productInfo=[_order.productList objectAtIndex:i];
        NSString *productId=[productInfo.productId stringValue];
        NSString *evaluate=[_contentArray objectAtIndex:i];
        NSString *productIntegral=[[_scoreArray objectAtIndex:i] stringValue];
        NSString *orderNo=_order.orderNo;
        
        NSDictionary *paramDic=[NSDictionary dictionaryWithObjectsAndKeys:
                                productId,@"productId",
                                evaluate,@"evaluate",
                                productIntegral,@"productIntegral",
                                orderNo,@"orderNo",
                                nil];
        [productList addObject:paramDic];
    }
    
    
    [self showLoadingHud:kLoading];
    [HRInterfaceManager evaluateOrderProducts:productList completion:^(BOOL isSucceed, NSString *message, id data) {
        [self hideLoadingHud];
        
        if (isSucceed) {
            //评价成功
            [self showSuccessHud:@"评价成功"];
            
            //更新订单
            [HRCommonData shareCommonData].hasNewOrder=YES;
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(HUD_DELAY_HIDE_SECOND * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self showTextHUD:message];
        }
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    
    int count=0;
    
    if (_order.productList!=nil && [_order.productList count]>0) {
        count = [_order.productList count];
    }
    
    return count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    // Return the number of rows in the section.
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HREvaluationCell *cell = (HREvaluationCell *)[tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER_EVALUATE];
    
    if (cell==nil) {
        cell=[[HREvaluationCell alloc]init];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.delegate=self;
    }
    
    cell.index=indexPath.row;
    
    HROrderProductInfo *productInfo=[_order.productList objectAtIndex:indexPath.row];
    
    CGFloat score=[[_scoreArray objectAtIndex:indexPath.row] floatValue];
    
    [cell setRating:score];
    
    [cell configWithData:productInfo];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    headerView.backgroundColor=[UIColor colorFromHexRGB:@"ECECEC"];
    
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CELL_HEIGHT_EVALUATE;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

#pragma mark - Evaluate Score Delegate Methods
//记录评价商品分数
-(void)evaluateProductScore:(CGFloat)score andIndex:(int)index{
    [_scoreArray replaceObjectAtIndex:index withObject:@(score)];
}

//评价商品内容
-(void)evaluateProductContent:(NSString *)content andIndex:(int)index{
    content=[content stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [_contentArray replaceObjectAtIndex:index withObject:content];
}


@end
